package com.moju.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.moju.common.Pagination;
import com.moju.mapper.BrandMapper;
import com.moju.pojo.Brand;
import com.moju.pojo.BrandExample;
import com.moju.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper mapper;

    @Override
    public Pagination getBrandByLikeAndDisplayWithLimit(String brandName, Integer isDisplay, Integer pageNo, Integer pageSize) {
        //mybatis分页插件
        PageHelper.startPage(pageNo,pageSize);

        StringBuffer sb = new StringBuffer();

        BrandExample brandExample = new BrandExample();

        brandExample.setOrderByClause(" id desc");

        BrandExample.Criteria criteria = brandExample.createCriteria();

        if(isDisplay == 1){
            criteria.andIsDisplayEqualTo(true);
            sb.append("isDisplay=").append(isDisplay);
        }else{
            criteria.andIsDisplayEqualTo(false);
            sb.append("isDisplay=").append(isDisplay);
        }

        if(brandName!=null && !"".equals(brandName)){
            criteria.andNameLike("%"+brandName+"%");
            sb.append("&brandName=").append(brandName);
        }

        List<Brand> brandList = mapper.selectByExample(brandExample);

        long count = mapper.countByExample(brandExample);

        Pagination pagination = new Pagination(pageNo,pageSize,(int)count,brandList);

        String url = "/brand/list";
        //设置分页样式
        pagination.pageView(url,sb.toString());

        System.out.println(pagination);

        return pagination;
    }

    @Override
    public int updateBrandById(Brand brand) {

        int i = mapper.updateByPrimaryKeySelective(brand);

        return i;
    }

    @Override
    public int insertBrand(Brand brand) {

        int i = mapper.insertSelective(brand);

        return i;
    }

    @Override
    public int deleteBrandById(Integer id) {

        int i = mapper.deleteByPrimaryKey(Long.valueOf(id));

        return i;
    }

    @Override
    public int deletesBrandsByIds(List ids) {

        BrandExample brandExample = new BrandExample();

        BrandExample.Criteria criteria = brandExample.createCriteria();

        criteria.andIdIn(ids);

        int i = mapper.deleteByExample(brandExample);

        return i;
    }

    @Override
    public Brand getBrandById(Integer id) {

        Brand brand = mapper.selectByPrimaryKey(Long.valueOf(id));

        return brand;
    }
}
